<?php
/**
 +------------------------------------------------------------------------------
 * Cookie管理类
 +------------------------------------------------------------------------------
 */
class ICookie
{ 
   /**
    +---------------------------------------------------------------------------
    * 判断Cookie是否存在
    +---------------------------------------------------------------------------
    * @param $name Cookie名字
    +---------------------------------------------------------------------------
    * @return void
    +---------------------------------------------------------------------------
    */
    static function is_set($name) {
        return isset($_COOKIE[$name]);
    }

   /**
    +---------------------------------------------------------------------------
    * 获取某个Cookie值
    +---------------------------------------------------------------------------
    * @param $name Cookie名字
    +---------------------------------------------------------------------------
    * @return Cookie的值
    +---------------------------------------------------------------------------
    */
    static function get($name) {
        if(!isset($_COOKIE[$name]) || false == $_COOKIE[$name]){
        	$value = '';
        }else{
        	$value = unserialize(base64_decode($_COOKIE[$name]));
        }
        return $value;
    }
    
   /**
    +---------------------------------------------------------------------------
    * 设置某个Cookie值
    +---------------------------------------------------------------------------
    * @param $name   Cookie名称
    * @param $value  Cookie值
    * @param $expire Coodie有效期
    * @param $path   Cookie路径
    * @param $domain Cookie有效域名
    +---------------------------------------------------------------------------
    * @return void
    +---------------------------------------------------------------------------
    */
    static function set($name,$value,$expire='',$path='',$domain='') {
        if($expire == '') {
            $expire = INI('cookie','expire');
        }
        if($path == '') {
            $path = INI('cookie','path');
        }
        if($domain == '') {
            $domain = INI('cookie','domain');
        }
        $expire = !empty($expire)?    time()+$expire   :  0;
        $value  = base64_encode(serialize($value));

        $cookie_name = $name;
        setcookie($cookie_name, $value,$expire,$path,$domain);
        $_COOKIE[$cookie_name] = $value;
    }

   /**
    +---------------------------------------------------------------------------
    * 删除某个Cookie值
    +---------------------------------------------------------------------------
    * @param $name   Cookie名称
    +---------------------------------------------------------------------------
    * @return void
    +---------------------------------------------------------------------------
    */
    static function delete($name) {
        self::set($name,'',-3600);
        unset($_COOKIE[$name]);
    }

   /**
    +---------------------------------------------------------------------------
    * 清空Cookie值
    +---------------------------------------------------------------------------
    * @return void
    +---------------------------------------------------------------------------
    */
    static function clear() {
        unset($_COOKIE);
    }
}
?>